﻿using BarcodeDelay.bean;
using BarcodeDelay.FrameDecoder;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BarcodeDelay.business
{
    public class DbMessageHandle
    {
        private StreamWriter swb = null;
        FileStream bFile = null;
        ArrayList m_arListCancelsBarcodes = new ArrayList();
        public event Action CancelsEvent;

        private static DbMessageHandle instance;
        public static DbMessageHandle Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DbMessageHandle();
                }
                return instance;
            }
        }
        private Thread fThreadDep = null;
        public void BeginAnalyThread()
        {
            try
            {
                initLog();
                m_arListCancelsBarcodes.Clear();
                if (fThreadDep != null && (fThreadDep.ThreadState == ThreadState.Running || fThreadDep.ThreadState == ThreadState.Background))
                {
                    fThreadDep.Abort();
                    System.Threading.Thread.Sleep(50);
                }
                if (fThreadDep != null && (fThreadDep.ThreadState == ThreadState.Running || fThreadDep.ThreadState == ThreadState.Background))
                {
                    while (fThreadDep.ThreadState != ThreadState.Aborted)
                    {
                        //当调用Abort方法后，如果thread线程的状态不为Aborted，主线程就一直在这里做循环，直到thread线程的状态变为Aborted为止
                        Thread.Sleep(100);
                    }
                }


                fThreadDep = new Thread(parseDBHandle);
                fThreadDep.Start();

                fThreadDep.IsBackground = true;
              

            }
            catch (Exception ex)
            {

            }
        }



        public void parseDBHandle()
        {
            StoParams.Instance.htResWechat.Clear();

            try
            {
                bool res=FrameDecoderManager.Instance.initQueRecentTask();
                if(!res)
                {
                    return;
                }
                while (true)
                {
                    ArrayList handleBeanList = Transaction.TransactionDatabase.Instance.getUnhandleMsg();
                    if(handleBeanList.Count>0)
                    {
                        foreach(HandleBean handleBean in handleBeanList)
                        {
                            string taskMsg = "";
                            string resMsg = "";
                            if (resMsg.Length > 13)
                            {
                                if(resMsg.Length<2048)
                                {
                                    Transaction.TransactionDatabase.Instance.addMsgContent(resMsg, taskMsg);
                                }
                                else
                                {
                                    string[] striparr = resMsg.Split(new string[] { "\r\n" }, StringSplitOptions.None);

                                    striparr = striparr.Where(s => !string.IsNullOrEmpty(s)).ToArray();
                                    StringBuilder sb = new StringBuilder();
                                    foreach(string strLine in striparr)
                                    {
                                        sb.AppendLine(strLine);
                                        if(sb.Length>2000)
                                        {
                                            Transaction.TransactionDatabase.Instance.addMsgContent(sb.ToString(), taskMsg);
                                            sb.Clear();
                                        }
                                    }
                                    if (sb.Length > 1)
                                    {
                                        Transaction.TransactionDatabase.Instance.addMsgContent(sb.ToString(), taskMsg);
                                        sb.Clear();
                                    }
                                        

                                    //string[] arr= resMsg.Split("\r\n")
                                }
                                                              
                            }
                            Transaction.TransactionDatabase.Instance.updateMsgFlag(handleBean.handleID.ToString());
                        }
                        
                    }
                    
                    Thread.Sleep(400);
                    
                }
                

            }
            catch (Exception ex)
            {
                

            }

           


        }






        public void stopThread()
        {
            try
            {
                ArrayList arListT = new ArrayList();
                ResultBean resultBean = new ResultBean();
                if (fThreadDep != null && (fThreadDep.ThreadState == ThreadState.Running || fThreadDep.ThreadState == ThreadState.Background))
                {
                    
                    fThreadDep.Abort();
                    if (swb != null)
                    {
                        swb.Close();
                    }

                }

    
            }
            catch (Exception ex)
            {

            }
        }


        private void initLog()
        {
            try
            {
                string dir = System.Environment.CurrentDirectory;
                bFile = new FileStream(dir + "\\error.txt", FileMode.Create);
                if (swb == null)
                {
                    swb = new StreamWriter(bFile);
                    swb.WriteLine("begin");

                }
            }
            catch (Exception ex)
            {

            }

        }


    }
}
